<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of FamilyController
 *
 * @author julien
 */
require_once 'Classes/Controllers/Controller.php';
require_once 'Classes/Model/FamilyModel.php';
class FamiliesController extends Controller {

    
    public function processRequest() {
        switch (HTTPRequestModel::getInstance()->extractGETValue('action')){
            case 'AddFamily':
                $this->addFamily();
                break;
            case 'EditFamily':
                $this->editFamily();
                break;
            case 'DeleteFamily':
                $this->deleteFamily();
                break;
            default:
                $this->listFamilies();
                break;
        }
    }
    
    public function editFamily(){
         if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "post"){
            $title= HTTPRequestModel::getInstance()->extractPOSTValue('Title');
            $id= HTTPRequestModel::getInstance()->extractGETValue('family');
            $family = new Family($id,$title);
            
            $error=0;
            if($title ==""){
                $error++;
            }
            
            
            if($error ==0){
                
                FamilyModel::getInstance()->editFamily($family);
                header('Location: /');
            }
            else {
                
                require_once 'Classes/View/Pages/FormBasedPage.php';

                
                $this->view = new FormBasedPage("Edit Family");
                $this->view->addCSS('/Ressources/style.css');
                $familyId = HTTPRequestModel::getInstance()->extractGETValue('family');
                $family = FamilyModel::getInstance()->getFamilyById($familyId);
                $this->view->addElement($family);
                $this->view->display();
                
            }
            
            
            
         }
         else if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "get") {
            require_once 'Classes/View/Pages/FormBasedPage.php';
            
            
            $this->view = new FormBasedPage("Edit Family");
            $this->view->addCSS('/Ressources/style.css');
            $familyId = HTTPRequestModel::getInstance()->extractGETValue('family');
            $family = FamilyModel::getInstance()->getFamilyById($familyId);
            $this->view->addElement($family);
            $this->view->display();
         }
     }
    
    
    
    public function addFamily(){
         if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "post"){
            $title= HTTPRequestModel::getInstance()->extractPOSTValue('Title');
            $id= HTTPRequestModel::getInstance()->extractGETValue('family');
            $family = new Family($id,$title);
            
            $error=0;
            if($title ==""){
                $error++;
            }
            
            
            if($error ==0){
                
                FamilyModel::getInstance()->addFamily($family);
                header('Location: /');
            }
            else {
                
                require_once 'Classes/View/Pages/FormBasedPage.php';

                
                $this->view = new FormBasedPage("Edit Family");
                $this->view->addCSS('/Ressources/style.css');
                $familyId = HTTPRequestModel::getInstance()->extractGETValue('family');
                $family = FamilyModel::getInstance()->getFamilyById($familyId);
                $this->view->addElement($family);
                $this->view->display();
                
            }
         }
         else if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "get") {
            require_once 'Classes/View/Pages/FormBasedPage.php';

            $this->view = new FormBasedPage("New Family");
            $this->view->addCSS('Ressources/style.css');
            $this->view->addElement(new Family());
            $this->view->display();
         }
     }
     
     
     public function deleteFamily(){
         $familyId =HTTPRequestModel::getInstance()->extractGETValue('family');
         FamilyModel::getInstance()->deleteFamilyById($familyId);
         
         header('Location: /');
     }
     
     public function listFamilies(){
         require_once 'Classes/View/Pages/ListBasedPage.php';
         if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "post"){
             
         }
         else if(strtolower(HTTPRequestModel::getInstance()->getRequest()) == "get") {
            $familyList;
            try{ 
                $familyList = FamilyModel::getInstance()->listFamily(); 
            }
            catch (NoObjectInDatabaseException $e){
                $familyList = null;
            }
            
            $this->view = new ListBasedPage("Families");
            $this->view->addCSS('Ressources/style.css');
            $this->view->setContentTitle("Families");
            $this->view->addElement($familyList);
            $this->view->display();
            
            
         }
     }
    
}

?>
